﻿using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace DocumentBuilder.Controls.Utils
{
    public class ControlBuilder
    {
        public static void ParseChildControls(Document context, XmlNode layoutNode, List<IControl> controlCollection)
        {
            if (layoutNode != null)
            {
                controlCollection.AddRange(from XmlNode childNode in layoutNode.ChildNodes 
                                           select CreateControl(context, childNode));
            }
        }

        private static IControl CreateControl(Document context, XmlNode controlNode)
        {
            var name = controlNode.Name.ToLower();

            if (name.Equals("repeater"))
                return Repeater.Create(context, controlNode);

            if (name.Equals("document"))
                return DocumentControl.Create(context, controlNode);

            if (name.Equals("#text"))
                return TextControl.Create(context, controlNode);

            return HtmlControl.Create(context, controlNode);
        }
    }
}